package com.ruoyi.project.stop.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.redis.RedisCache;
import com.ruoyi.common.redis.RedisConst;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.stop.mapper.ShipRecordMapper;
import com.ruoyi.project.stop.domain.ShipRecord;
import com.ruoyi.project.stop.service.IShipRecordService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 船舶信息录入Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-26
 */
@Service
public class ShipRecordServiceImpl implements IShipRecordService
{
    @Autowired
    private ShipRecordMapper shipRecordMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询船舶信息录入
     *
     * @param id 船舶信息录入主键
     * @return 船舶信息录入
     */
    @Override
    public ShipRecord selectShipRecordById(Long id)
    {
        return shipRecordMapper.selectShipRecordById(id);
    }

    @Override
    public ShipRecord selectShipRecordByIdLast()
    {
        ShipRecord shipRecord = new ShipRecord();
        String redisStr =  JSON.toJSONString(redisCache.getCacheObject(RedisConst.SHIP_LAST_INFO));
        // 查询redis中的最新数据
        if(ObjectUtil.isNotEmpty(redisStr) &&  !"null".equals(redisStr)){
            shipRecord = JSON.parseObject(redisStr, ShipRecord.class);
        }else {
            shipRecord = shipRecordMapper.selectShipRecordByIdLast();
            redisCache.setCacheObject(RedisConst.SHIP_LAST_INFO, shipRecord, 1, TimeUnit.HOURS);
        }
        return shipRecord;
    }

    /**
     * 查询船舶信息录入列表
     *
     * @param shipRecord 船舶信息录入
     * @return 船舶信息录入
     */
    @Override
    public List<ShipRecord> selectShipRecordList(ShipRecord shipRecord)
    {
        return shipRecordMapper.selectShipRecordList(shipRecord);
    }

    /**
     * 新增船舶信息录入
     *
     * @param shipRecord 船舶信息录入
     * @return 结果
     */
    @Override
    public int insertShipRecord(ShipRecord shipRecord)
    {
        shipRecord.setCreateTime(DateUtils.getNowDate());
        int count = shipRecordMapper.insertShipRecord(shipRecord);
        Long shipId = shipRecord.getId();
        if(shipId != null){
            //保存redis
            redisCache.setCacheObject(RedisConst.SHIP_LAST_INFO, shipRecord, 1, TimeUnit.HOURS);
        }
        return count;
    }

    /**
     * 修改船舶信息录入
     *
     * @param shipRecord 船舶信息录入
     * @return 结果
     */
    @Override
    public int updateShipRecord(ShipRecord shipRecord)
    {
        shipRecord.setUpdateTime(DateUtils.getNowDate());
        return shipRecordMapper.updateShipRecord(shipRecord);
    }

    /**
     * 批量删除船舶信息录入
     *
     * @param ids 需要删除的船舶信息录入主键
     * @return 结果
     */
    @Override
    public int deleteShipRecordByIds(String ids)
    {
        return shipRecordMapper.deleteShipRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除船舶信息录入信息
     *
     * @param id 船舶信息录入主键
     * @return 结果
     */
    @Override
    public int deleteShipRecordById(Long id)
    {
        return shipRecordMapper.deleteShipRecordById(id);
    }
}
